package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.mapper.SpuMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SpecParamService paramService;


    @Override
    public List<SpecParamDTO> listSpecParamBySpuId(Long id, Boolean searching) {
        SpuDetail spuDetail = this.getById(id);
        Map<Long, Object> map = JsonUtils.toMap(spuDetail.getSpecification(), Long.class, Object.class);
        Spu spu = spuService.getById(id);
        List<SpecParamDTO> specParamDTOS = paramService.querySpecParams(spu.getCid3(), null, searching);
        specParamDTOS.forEach(specParamDTO -> specParamDTO.setValue(
                map.get(specParamDTO.getId())
        ));

        return specParamDTOS;
    }
}
